package com.dwy2002.room.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dwy2002.common.Result;
import com.dwy2002.common.exception.EmptyIdException;
import com.dwy2002.common.utils.JwtUtil;
import com.dwy2002.common.utils.Utils;
import com.dwy2002.enums.UniversalStatus;
import com.dwy2002.pojo.po.rooms.Reservations;
import com.dwy2002.pojo.po.rooms.Rooms;
import com.dwy2002.pojo.po.users.Users;
import com.dwy2002.pojo.vo.rooms.RoomsVO;
import com.dwy2002.room.mapper.ReservationMapper;
import com.dwy2002.room.mapper.RoomsMapper;
import com.dwy2002.room.service.IReservationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static com.dwy2002.common.Result.success;
import static com.dwy2002.common.utils.Utils.getToken;
import static com.dwy2002.common.utils.Utils.isEmptyOrNull;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/02/13
 */
@Service
@RequiredArgsConstructor
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservations> implements IReservationService {
    private final JwtUtil jwtUtil;
    private final RoomsMapper roomsMapper;

    /**
     * 检测当前用户是否是重复预约该包间的时间
     *
     * @param id    包间id
     * @param dates 预约时间
     * @return true:数据库中有该用户预约的时间 false:没有
     * @author 杜伟毅
     * @since 2025/2/13 0013 0:17
     */
    @Override
    public boolean isUserDuplicateReserve(String id, List<LocalDateTime> dates) {
        if (Utils.isListEmpty(dates) || isEmptyOrNull(id)) throw new EmptyIdException("isUserDuplicateReserve");

        String userId = jwtUtil.parseToken(getToken(), Users.class).getId();
        LambdaQueryWrapper<Reservations> lqw = new LambdaQueryWrapper<>();

        for (LocalDateTime date : dates) {
            lqw.eq(Reservations::getRoomId, id)
                    .eq(Reservations::getUserId, userId)
                    .eq(Reservations::getReservationDate, date)
                    .eq(Reservations::getStatus, UniversalStatus.TWO.getNum())
                    .eq(Reservations::getPaymentStatus, UniversalStatus.TWO.getNum());
            Reservations reservation = getOne(lqw);
            if (reservation != null) {
                return true;
            }
        }
        return false;
    }

    /**
     * 删除用户预约的包间
     *
     * @param id   包间id
     * @param date 预约日期
     * @return true：删除成功，false：删除失败
     * @author 杜伟毅
     * @since 2025/2/14 0014 21:47
     */
    @Override
    public boolean removeReservation(String id, LocalDateTime date) {
        LambdaQueryWrapper<Reservations> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Reservations::getRoomId, id)
                .eq(Reservations::getUserId, jwtUtil.parseToken(getToken(), Users.class).getId())
                .eq(Reservations::getReservationDate, date)
                .eq(Reservations::getStatus, UniversalStatus.THREE.getNum())
                .eq(Reservations::getPaymentStatus, UniversalStatus.THREE.getNum());
        return remove(lqw);
    }

    /**
     * 返回用户全部预约包间
     *
     * @author 杜伟毅
     * @since 2025/4/2 0002 0:24
     */
    @Override
    public Result<List<RoomsVO>> getBookedRooms() {
        String userId = jwtUtil.parseToken(getToken(), Users.class).getId();
        LambdaQueryWrapper<Reservations> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Reservations::getUserId, userId);
        List<String> roomIds = list(lqw).stream()
                .map(Reservations::getRoomId)
                .toList();
        List<Rooms> rooms = roomsMapper.selectByIds(roomIds);
        return success(BeanUtil.copyToList(rooms, RoomsVO.class));
    }
}
